/*
 * Reverse Integer
Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321
 */
package com.xinpan.exercise;

public class ReverseInteger {
    public int reverse(int x) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if(x == 0)
            return x;
            
        boolean pos = true;
        if(x < 0)
            pos = false;
        String str = "";
        
        x = Math.abs(x);
        while(x > 0)
        {
            str += (x % 10);
            x /= 10;
        }
        int res = Integer.valueOf(str);
        
        if(!pos)
            res = 0 - res;
        
        return res;
    }
}
